﻿using System.Collections.Generic;
using Entity;
using System.Linq;
using Utils;
using System;
using DataBaseConnect.IRepositories;

namespace DataBaseConnect.App
{
    public class MenuAppService : IMenuAppService
    {

        //菜单管理仓储接口
        private readonly IMenuRepository _menuRepository;
        private readonly IButtonRepository _buttonRepository;

        /// <summary>
        /// 构造函数 实现依赖注入
        /// </summary>
        /// <param name="userRepository">仓储对象</param>
        public MenuAppService(IMenuRepository menuRepository, IButtonRepository buttonRepository)
        {
            _menuRepository = menuRepository;
            _buttonRepository = buttonRepository;
        }

        /// <summary>
        /// 获取菜单(分页)
        /// </summary>
        /// <param name="MenuName">菜单名</param>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页显示多少条</param>
        /// <param name="rowCount">总条数</param>
        /// <returns></returns>
        public List<Menu> GetMenuListPage(string MenuName, int page, int limit, out int rowCount)
        {
            if (string.IsNullOrEmpty(MenuName))
            {
                return _menuRepository.LoadPageListOne(page, limit, out rowCount, null, f => f.MenuId);
            }
            else
            {
                return _menuRepository.LoadPageListOne(page, limit, out rowCount, f => f.MenuName == MenuName, f => f.MenuId).ToList();
            }
        }

        /// <summary>
        ///  添加菜单信息
        /// </summary>
        /// <param name="input">菜单信息实体</param>
        /// <returns></returns>
        public ReceiveStatus AddMenuMsg(Menu input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _menuRepository.Insert(input, true);
                receiveStatus.msg = "新增成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        ///  编辑菜单信息
        /// </summary>
        /// <param name="input">菜单信息实体</param>
        /// <returns></returns>
        public ReceiveStatus EditMenuMsg(Menu input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _menuRepository.Update(input);
                receiveStatus.msg = "编辑成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 递归删除菜单
        /// 删除菜单权限
        /// 删除菜单按钮权限
        /// 删除菜单按钮
        /// </summary>
        /// <param name="MenuId">菜单主键</param>
        /// <returns></returns>
        public ReceiveStatus DelMenu(int MenuId)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                DelForMneuMsg(MenuId); //递归删除菜单树
                receiveStatus.msg = "删除成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 递归删除菜单
        /// </summary>
        /// <param name="MenuId">菜单id</param>
        /// <returns></returns>
        public ReceiveStatus DelForMneuMsg(int MenuId)
        {
            List<Menu> list = new List<Menu>();
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                var ListId = _menuRepository.GetAllList(f => f.MenuParentId == MenuId);
                //删除节点
                _menuRepository.Delete(f => f.MenuId == MenuId);

                if (ListId.Count != 0)
                {
                    //删除下级菜单
                    foreach (var item in ListId)
                    {
                        _menuRepository.DelMenuRoleMsg(f => f.MenuId == item.MenuId);//删除菜单权限
                        _buttonRepository.DelButtonRoleMsg(r => r.MenuId == item.MenuId); // 删除菜单按钮权限
                        _buttonRepository.Delete(r => r.MenuId == item.MenuId); //删除菜单按钮
                        _menuRepository.Delete(f => f.MenuId == item.MenuId); // 删除下级菜单
                        list.AddRange(GetForMenuListMsg(item.MenuId)); // 当前菜单的下级菜单集合
                        receiveStatus.msg = "删除成功";
                    }
                }
                ListId = list; // 最新集合
                foreach (var item in ListId)
                {
                    DelForMneuMsg(item.MenuId); // 递归
                }
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "删除失败" + ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 获取下级菜单集合
        /// </summary>
        /// <param name="MenuId">菜单id</param>
        /// <returns></returns>
        public List<Menu> GetForMenuListMsg(int MenuId)
        {
            return _menuRepository.GetAllList(f => f.MenuParentId == MenuId);
        }

        /// <summary>
        /// 根据主键获取菜单信息
        /// </summary>
        /// <param name="MenuId">主键</param>
        /// <returns></returns>
        public Menu GetMenuFirst(int MenuId)
        {
            return _menuRepository.FirstOrDefault(f => f.MenuId == MenuId);
        }

        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
        public List<Menu> GetAllMenuList()
        {
            return _menuRepository.GetAllList();
        }

        /// <summary>
        /// 根据角色id获得菜单权限
        /// </summary>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        public List<MenuRole> GetRoleIdList(int RoleId)
        {
            return _menuRepository.GetRoleIdList(RoleId);
        }
    }
}
